package com.hualu.zks.CommonUtils;

import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


/**
 * 给JFrame设置背景图片
 * @author Ma Yong
 */
public class ZZBackGround {
	private static Map<JFrame,ZZBackGround> data = new HashMap<JFrame,ZZBackGround>();
	
	public static final String FILL = "填充";
	public static final String NORMAL = "常规";
	
	private JFrame frame;//背景所属的frame
	private ImageIcon icon = null;//背景图片
	private JLabel label = new JLabel();//图片框
	private String scaleType = NORMAL;//显示类型
        private JPanel imagePanel;
	
	/**
	 * 给JFrame设置背景图片
	 * @param frame 目标窗体
	 * @param imgPath 图片路径
	 * @param scaleType 缩放类型：[FILL:填充,不支持动图][NORMAL:常规,支持动图]
	 */
	public static void setBackBround(final JFrame frame,final String imgPath, final String scaleType){
		ZZBackGround bgi = null;
		if(data.containsKey(frame)){
			bgi = data.get(frame);
		}else{
			bgi = new ZZBackGround();
			frame.addComponentListener(new SizeListener(bgi));
			//new SizeThread(bgi).start();
		}
		bgi.frame = frame;
		bgi.icon = new ImageIcon(imgPath);
		bgi.scaleType = scaleType;
		
		core(bgi);//设置背景
	}
	
	/**
	 * 给JFrame设置背景图片
	 * @param frame 目标窗体
	 * @param image 背景图像
	 * @param scaleType 缩放类型：[FILL:填充,不支持动图][NORMAL:常规,支持动图]
	 */
	public static void setBackBround(final JFrame frame,final Image image, final String scaleType){
		ZZBackGround bgi = null;
		if(data.containsKey(frame)){
			bgi = data.get(frame);
		}else{
			bgi = new ZZBackGround();
			frame.addComponentListener(new SizeListener(bgi));
			//new SizeThread(bgi).start();
		}
		bgi.frame = frame;
		bgi.icon = new ImageIcon(image);
		bgi.scaleType = scaleType;
		
		core(bgi);//设置背景
	}

	
	//设置背景的核心代码
	private static void core(final ZZBackGround bgi){
		bgi.frame.setVisible(true);
		// 把内容窗格转化为JPanel，否则不能用方法setOpaque()来使内容窗格透明
		JPanel imagePanel = (JPanel) bgi.frame.getContentPane();
		imagePanel.setOpaque(false);
		sizeAndPosition(bgi);
		bgi.frame.getLayeredPane().setLayout(null);
		// 把背景图片添加到分层窗格的最底层作为背景
		bgi.frame.getLayeredPane().add(bgi.label,new Integer(Integer.MIN_VALUE));
	}
	
	//计算label和image的尺寸并将image设置到label中
	private static void sizeAndPosition(final ZZBackGround bgi){
		if (NORMAL.equals(bgi.scaleType)) {
                    bgi.label.setBounds(0, 0, bgi.icon.getIconWidth(), bgi.icon.getIconHeight());
//                    // 把内容窗格转化为JPanel，否则不能用方法setOpaque()来使内容窗格透明
//                    bgi.imagePanel = (JPanel) bgi.frame.getContentPane();
//                    bgi.imagePanel.setOpaque(false);
//                    // 内容窗格默认的布局管理器为BorderLayout
//                    bgi.frame.getLayeredPane().setLayout(null);
//                    // 把背景图片添加到分层窗格的最底层作为背景
//                    bgi.frame.getLayeredPane().add(bgi.label, new Integer(Integer.MIN_VALUE));
                      bgi.label.setIcon(bgi.icon);
		} else if (FILL.equals(bgi.scaleType)) {
			//int w = bgi.frame.getContentPane().getWidth();
			//int h = bgi.frame.getContentPane().getHeight();
                    int w = bgi.frame.getSize().width;
                    int h = bgi.frame.getSize().height;
			bgi.label.setBounds(0, 0, w, h);
			Image img = bgi.icon.getImage().getScaledInstance(w, h,Image.SCALE_FAST);
			bgi.label.setIcon(new ImageIcon(img));
		}
	}
	
	//当frame改变大小时重新计算label和image
	private static class SizeListener implements ComponentListener{
		ZZBackGround bgi = null;
		public SizeListener(ZZBackGround bgi) {
			this.bgi = bgi;
		}
		public void componentResized(ComponentEvent e) {
			ZZBackGround.sizeAndPosition(bgi);
		}
		public void componentMoved(ComponentEvent e) {}
		public void componentShown(ComponentEvent e) {}
		public void componentHidden(ComponentEvent e) {}
	}
	
	private static class SizeThread extends Thread{
		ZZBackGround bgi = null;
		int w=0;
		int h=0;
		int n=0;
		public SizeThread(ZZBackGround bgi) {
			this.bgi = bgi;
		}
		@Override
		public void run() {
			while(true){
				try {
					if(bgi.frame.getWidth()!=w||bgi.frame.getHeight()!=h){
						ZZBackGround.sizeAndPosition(bgi);
						w = bgi.frame.getWidth();
						h = bgi.frame.getHeight();
						System.out.println(++n);
					}
					this.sleep(33);
					
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
	}

}
